using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class UserPostUploadDal : Base
    {
        public enum Fields
        {
            Id,
            UserId,
            UserName,
            PostId,
            FileId,
            Title,
            Comment,
            IPAddress,
            Reviewed,
            Reported,
            Suspended,
            Created,
            VoteAverage,
            VoteCount,
            VoteTotal,
            TextFields
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.UserId, reader, table);
            GetReaderValue<string>(Fields.UserName, reader, table);
            GetReaderValue<int>(Fields.PostId, reader, table);
            GetReaderValue<int>(Fields.FileId, reader, table);
            GetReaderValue<string>(Fields.Title, reader, table);
            GetReaderValue<string>(Fields.Comment, reader, table);
            GetReaderValue<System.Net.IPAddress>(Fields.IPAddress, reader, table);
            GetReaderValue<bool>(Fields.Reviewed, reader, table);
            GetReaderValue<bool>(Fields.Reported, reader, table);
            GetReaderValue<bool>(Fields.Suspended, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
            GetReaderValue<decimal>(Fields.VoteAverage, reader, table);
            GetReaderValue<decimal>(Fields.VoteTotal, reader, table);
            GetReaderValue<int>(Fields.VoteCount, reader, table);
            GetReaderValue<System.Xml.XmlDocument>(Fields.TextFields, reader, table);
        }

        public void Update(ref int id, int userId, ref string userName, int postId, int fileId, string title, string comment, System.Net.IPAddress ipAddress, bool reviewed, bool reported, bool suspended, DateTime created, ref decimal voteAverage, ref decimal voteTotal, ref int voteCount, System.Xml.XmlDocument textFields, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "PostId", postId, SqlDbType.Int);
            AddInputParameter(cmd, "FileId", fileId, SqlDbType.Int);
            AddInputParameter(cmd, "Title", title, SqlDbType.VarChar, 500);
            AddInputParameter(cmd, "Comment", comment, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "IPAddress", ipAddress, SqlDbType.VarChar, 16);
            AddInputParameter(cmd, "Reviewed", reviewed, SqlDbType.Bit);
            AddInputParameter(cmd, "Reported", reported, SqlDbType.Bit);
            AddInputParameter(cmd, "Suspended", suspended, SqlDbType.Bit);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime);
            AddInputParameter(cmd, "TextFields", textFields, SqlDbType.Xml);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "UserNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "VoteAverageOut", SqlDbType.Decimal, null, 4, 5);
            AddOutputParameter(cmd, "VoteTotalOut", SqlDbType.Decimal, null, 4, 5);
            AddOutputParameter(cmd, "VoteCountOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            userName = GetOutputValue<string>(cmd, "UserNameOut");
            voteAverage = GetOutputValue<decimal>(cmd, "VoteAverageOut");
            voteTotal = GetOutputValue<decimal>(cmd, "VoteTotalOut");
            voteCount = GetOutputValue<int>(cmd, "VoteCountOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByUserIdAndPostId(int userId, int postId)
        {
            SqlCommand cmd = CreateCommand("GetAllByUserIdAndPostId");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "PostId", postId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByPostId(int postId)
        {
            SqlCommand cmd = CreateCommand("GetAllByPostId");
            AddInputParameter(cmd, "PostId", postId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

    }
}
